<?php
class User extends CI_Model {
	function __construct() {
		parent::__construct();
	}
	
	function get($where = null){
		$this->db->select('*');
		$this->db->from('petugas');
		if($where != NULL){
			$this->db->where($where);
		}
		return $this->db->get();
	}
	
	function get_hash($username, $password) {
		return md5($username.':'.$password);
	}

	function add($data) {
		$data['password'] = $this->get_hash($data['username'], $data['password']);
		$this->db->insert('petugas', $data);
		return $this->db->insert_id();
	}
	
	function edit($id,$data) {
		if(isset($data['password']) && $data['password'] != NULL){
			$data['password'] = $this->get_hash($data['username'], $data['password']);
		}
		$this->db->where('id_petugas',$id);
		return $this->db->update('petugas', $data);
	}

	function auth($username, $password, $where = NULL) {
		$this->db->select('petugas.*');
		$this->db->from('petugas');
		$this->db->where(array(
			'petugas.username' 		 => $username,
			'petugas.password' 		 => $this->get_hash($username, $password),
		));
		if($where != NULL){
			$this->db->where($where);
		}
		return $this->db->get()->row_array();
	}

	function delete($id){
		$this->db->where('id_petugas',$id);
		return $this->db->delete('petugas');
	}
}
